package zhihui.mobile.ui.shop.sales;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zgm.utils.DateUtils;
import com.zgm.utils.ui.UiHeaderLayout;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.sales.SalesDesc;
import zhihui.mobile.bean.shop.sales.SalesListData;
import zhihui.mobile.bean.shop.sales.SalesListSkuData;
import zhihui.mobile.bean.shop.sales.SalesShopInfo;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.ShopSalesPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/8/2 0002.
 */

public class ShopSalesActivity extends BaseActivity implements IShopSalesContract.IShopSalesView
{
    private IShopSalesContract.IShopSalesPresenter salesPresenter = new ShopSalesPresenter();

    private UiHeaderLayout mheader;
    private LinearLayout ly_sessions;
    private PullToRefreshListView lv_content;

    private SalesAdapter salesAdapter;
    private int type = 1;
    private TextView curTime;
    private TextView cueDesc;
    @Override
    public void initView()
    {
        mheader = (UiHeaderLayout) findViewById(R.id.header_shop_sales);
        switch (type)
        {
            case 1:
                mheader.setTitle("限时抢购");
                break;
            case 2:
                mheader.setTitle("超级秒杀");
                break;
        }
        mheader.setRootBackgroundColor("#fafafa");
        mheader.setTitleStyle(R.style.ShopSalesTitleStyle);
        mheader.setLeftBottonIcon(R.drawable.shop_back);
        mheader.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(ShopSalesActivity.class);
            }
        });
        mheader.hindRightOneButton();
        mheader.hindRightTwoButton();

        ly_sessions = (LinearLayout) findViewById(R.id.ly_shop_sales_session);
        lv_content = (PullToRefreshListView) findViewById(R.id.lv_shop_sales_content);
        salesAdapter = new SalesAdapter(this);
        salesAdapter.setListener(new SalesAdapter.IButtonBuyListener()
        {
            @Override
            public void buy(SalesShopInfo shopIno, int salesId)
            {
                salesPresenter.goConfirmOrder(shopIno, salesId);
            }
        });
        lv_content.setAdapter(salesAdapter);
        salesPresenter.initData();
    }

    @Override
    protected void parseArgumentsFormIntent(Intent argIntent)
    {
        type = argIntent.getIntExtra("salesType", 1);
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shop_sales_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{salesPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        salesPresenter.init(this, this);
    }

    public void refreshList(SalesListData data)
    {
        salesAdapter.setMlist(data.getTails().getSku());
        salesAdapter.setCurSalesInfo(data);
        salesAdapter.notifyDataSetChanged();
    }

    @Override
    public void initDataView(List<SalesListData> listDatas)
    {
        ly_sessions.setWeightSum(listDatas.size());

        for (int i = 0; i < listDatas.size(); i++)
        {
            final SalesListData sales = listDatas.get(i);
            View view = LayoutInflater.from(this).inflate(R.layout.shop_sales_session_item, ly_sessions, false);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
            view.setLayoutParams(layoutParams);
            final TextView tv_time = (TextView) view.findViewById(R.id.tv_shop_sales_session_time);
            tv_time.setText(DateUtils.getHour(sales.getStarttime()));
            final TextView tv_desc = (TextView) view.findViewById(R.id.tv_shop_sales_session_desc);
            if (!DateUtils.isBefor(sales.getStarttime()))
            {
                tv_desc.setText("抢购中");
            } else
            {
                tv_desc.setText("即将开始");
            }
            if (i == 0)
            {
                tv_time.setTextColor(getResources().getColor(R.color.shop_text_select));
                tv_desc.setTextColor(getResources().getColor(R.color.shop_text_select));
                curTime = tv_time;
                cueDesc = tv_desc;
                refreshList(sales);
            }
            view.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    refreshList(sales);
                    tv_time.setTextColor(getResources().getColor(R.color.shop_text_select));
                    tv_desc.setTextColor(getResources().getColor(R.color.shop_text_select));
                    curTime.setTextColor(Color.parseColor("#333333"));
                    cueDesc.setTextColor(Color.parseColor("#333333"));
                    curTime = tv_time;
                    cueDesc = tv_desc;
                }
            });
            ly_sessions.addView(view);
        }
    }
}
